package com.sgb.kjwl.view.ui.fragment.self;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentGoodsState3Binding;
import com.sgb.kjwl.di.self.DaggerSelfFragComponent;
import com.sgb.kjwl.di.self.SelfFragModule;
import com.sgb.kjwl.model.entity.self.GoodsListEntity3;
import com.sgb.kjwl.view.ui.adapter.self.publish.GoodsStateAdapter3;
import com.sgb.kjwl.view.widget.GoodsPriceDialog3;
import com.sgb.kjwl.viewmodel.self.publish.GoodsStateModel3;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Description:
 * Author zhengkewen
 */
public class GoodsStateFragment3 extends AppFragment {
    public static final String TAG = "";
    public static final String KEY_TYPE = "type";
    public static final String STATUS_UP = "1";
    public static final String STATUS_DOWN = "2";
    public static final String OPT_UP = "1";
    public static final String OPT_DOWN = "2";
    public static final String OPT_DEL = "3";
    public static final String TYPE_SHANGTONG = "7";
    private List<GoodsListEntity3> datas;

    private FragmentGoodsState3Binding binding;
    @Inject
    GoodsStateModel3 viewModel;

    private String status = "";
    private GoodsStateAdapter3 goodsListAdapter3;
    private List<GoodsListEntity3> goodsListEntity3List;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser) {
            if (viewModel != null) {
                viewModel.getGoodsListModel(false);
            }
        }
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerSelfFragComponent.builder()
                .appComponent(appComponent)
                .selfFragModule(new SelfFragModule(this))
                .build()
                .inject(this);
    }

    public static GoodsStateFragment3 newInstance(String type) {
        GoodsStateFragment3 fragment = new GoodsStateFragment3();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_goods_state3, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        binding.modifyView.setOnClickListener(this);
        binding.upView.setOnClickListener(this);
        binding.downView.setOnClickListener(this);
        binding.delView.setOnClickListener(this);
        binding.closeView.setOnClickListener(this);
        if (goodsListAdapter3 == null) {
            goodsListAdapter3 = new GoodsStateAdapter3(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.rlvView.setLayoutManager(layoutManager);
        binding.rlvView.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getResources().getDimension(R.dimen.dp1), getResources().getColor(R.color.color_F5F5F5)));
        binding.rlvView.setAdapter(goodsListAdapter3);
        binding.rlvView.setLoadingMoreEnabled(true);
        binding.rlvView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getGoodsListModel(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getGoodsListModel(true);
            }
        });
        goodsListAdapter3.setMultiSelect(true);
        goodsListAdapter3.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<GoodsListEntity3>() {
            @Override
            public void onItemMultiClick(List<GoodsListEntity3> goodsListEntity3s, int position) {

                if (goodsListEntity3s == null) {
                    return;
                }
               /* if (datas.get(position).isPromote()) {
                    Toast.makeText(getActivity(), "促销商品暂不能操作", Toast.LENGTH_SHORT).show();
                    goodsListEntity3s.remove((datas.get(position)));
                }*/
                if (goodsListEntity3s.size() == 0) {
                    binding.checkView.setSelected(false);
                }
                if (goodsListEntity3s.size() == goodsListAdapter3.getItemCount()) {
                    binding.checkView.setSelected(true);
                }
                goodsListEntity3List = goodsListEntity3s;
                binding.countView.setText(String.valueOf(goodsListEntity3List.size()));
            }
        });
        binding.checkView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                binding.checkView.setSelected(isChecked);
                if (isChecked) {
                    goodsListAdapter3.checkALLDatas();
                } else {
                    goodsListAdapter3.resetCheckDatas();
                }
                goodsListEntity3List = goodsListAdapter3.getCheckDatas();
                binding.countView.setText(String.valueOf(goodsListEntity3List.size()));
            }
        });
    }

    private void initData() {
        status = (String) getArguments().get(KEY_TYPE);
        if (status == STATUS_UP) {
            binding.upView.setVisibility(View.GONE);
            binding.downView.setVisibility(View.VISIBLE);
            binding.modifyView.setVisibility(View.GONE);
        } else {
            binding.downView.setVisibility(View.GONE);
            binding.modifyView.setVisibility(View.GONE);
            binding.upView.setVisibility(View.VISIBLE);
        }
        viewModel.status.set(status);
        viewModel.getGoodsListModel(false);
    }

    public void showList(List<GoodsListEntity3> datas, boolean isLoadMore) {
        this.datas = datas;
        if (isLoadMore) {
            binding.rlvView.loadMoreComplete();
            if (datas.size() != 0) {
                goodsListAdapter3.addDatas(datas);
            }
        } else {
            if (datas.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.rlvView.refreshComplete();
            binding.rlvView.setLoadingMoreEnabled(true);
            goodsListAdapter3.setDatas(datas);
            resetChooseState();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.modify_view:
                modifyGoodsPrice();
                break;
            case R.id.up_view:
                getGoodsIds(OPT_UP);
                break;
            case R.id.down_view:
                getGoodsIds(OPT_DOWN);
                break;
            case R.id.del_view:
                getGoodsIds(OPT_DEL);
                break;
            case R.id.close_view:
                binding.topLayout.setVisibility(View.GONE);
                break;
        }
    }

    private void getGoodsIds(String opt) {
        List<String> goodsIds = new ArrayList<>();
        if (goodsListEntity3List != null) {
            for (GoodsListEntity3 goodsListEntity3 : goodsListEntity3List) {
                goodsIds.add(goodsListEntity3.getId());
            }
        }
        if (goodsIds.size() == 0) {
            MToast.showToast(getActivity(), "请先选择商品!");
            return;
        }
        if (opt.equals(OPT_UP)) {
            String msg = "共选择" + goodsIds.size() + "款商品\n商品即将上架对外出售";
            new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                    .setTitle("温馨提示")
                    .setMsg(msg)
                    .setDialogCallBack(new CommonDialog.DialogCallBack() {
                        @Override
                        public void onRightClick() {
                            viewModel.upDowngetGoodsModel(opt, goodsIds);
                        }

                        @Override
                        public void onLeftClick() {

                        }
                    }).show();
        } else if (opt.equals(OPT_DOWN)) {
            String msg = "共选择" + goodsIds.size() + "款商品\n商品下架后，将不在平台出售";
            new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                    .setTitle("温馨提示")
                    .setMsg(msg)
                    .setDialogCallBack(new CommonDialog.DialogCallBack() {
                        @Override
                        public void onRightClick() {
                            viewModel.upDowngetGoodsModel(opt, goodsIds);
                        }

                        @Override
                        public void onLeftClick() {

                        }
                    }).show();
        } else if (opt.equals(OPT_DEL)) {
            String msg = "共选择" + goodsIds.size() + "款商品\n删除不可恢复，是否确定删除？";
            new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                    .setTitle("温馨提示")
                    .setMsg(msg)
                    .setDialogCallBack(new CommonDialog.DialogCallBack() {
                        @Override
                        public void onRightClick() {
                            viewModel.upDowngetGoodsModel(opt, goodsIds);
                        }

                        @Override
                        public void onLeftClick() {

                        }
                    }).show();
        }
    }

    private void modifyGoodsPrice() {
        boolean edit = true;
        List<String> goodsIds = new ArrayList<>();
        float minPrice = 0;
        if (goodsListEntity3List == null || goodsListEntity3List.size() == 0) {
            MToast.showToast(getActivity(), "请先选择商品!");
            return;
        }
        minPrice = Float.parseFloat(goodsListEntity3List.get(0).getPriceScope());
        for (GoodsListEntity3 goodsListEntity3 : goodsListEntity3List) {
            goodsIds.add(goodsListEntity3.getId());
            if (Float.parseFloat(goodsListEntity3.getPrice()) < minPrice) {
                minPrice = Float.parseFloat(goodsListEntity3.getPriceScope());
            }
            if (goodsListEntity3.getCid1().equals(TYPE_SHANGTONG)) {
                edit = false;
            }
        }
        if (!edit) {
            binding.topLayout.setVisibility(View.VISIBLE);
            return;
        }
        new GoodsPriceDialog3(getActivity(), null)
                .setMinPrice(minPrice)
                .setDialogClickCallBack(new GoodsPriceDialog3.DialogClickCallBack() {
                    @Override
                    public void onClickResult(String price, String type) {
                        viewModel.modifyGoodsPriceModel(price, type, goodsIds);
                    }
                }).show();
    }

    private void resetChooseState() {
        if (goodsListEntity3List != null) {
            goodsListEntity3List.clear();
        }
        binding.countView.setText(String.valueOf(0));
        goodsListAdapter3.resetCheckDatas();
        binding.checkView.setSelected(false);
    }

    public void showOptSuccess(String opt, int size) {
        String msg = "";
        if (opt.equals(OPT_UP)) {
            msg = "上架" + size + "款商品成功";
        } else if (opt.equals(OPT_DOWN)) {
            msg = "下架" + size + "款商品成功";
        } else if (opt.equals(OPT_DEL)) {
            msg = "删除" + size + "款商品成功";
        }
        MToast.showToast(getActivity(), msg);
        viewModel.getGoodsListModel(false);
    }
}
